﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmos.Hardware.BlockDevice;
using Sys = Cosmos.System;
using FAT = Cosmos.System.Filesystem.FAT;


namespace ArkadiaOsKernel.Core.Filesystem.Fat
{
    class FileSupport
    {

        public AtaPio xATA = null;
        public Partition ArkadiaPartition = null;
        public FAT.FatFileSystem FATFS = null;
        public List<Sys.Filesystem.Listing.Base> FATFileList;
        public string partition = "C";
        public void Init()
        {


            try
            {
                //Look through all of the devices and see if you can find a hard drive
                for (int i = 0; i < BlockDevice.Devices.Count; i++)
                {
                    if (BlockDevice.Devices[i] is AtaPio)
                    {
                        //Set our device to the hard drive
                        xATA = (AtaPio)BlockDevice.Devices[i];
                    }
                }
                //Look through all of the devices and look for a partition
                for (int j = 0; j < BlockDevice.Devices.Count; j++)
                {
                    if (BlockDevice.Devices[j] is Partition)
                    {
                        ArkadiaPartition = (Partition)BlockDevice.Devices[j];
                    }
                }
                //Make a filesystem attached to the partition
                FATFS = new FAT.FatFileSystem(ArkadiaPartition);
                //Map the filesystem to the partition drive
                Cosmos.System.Filesystem.FileSystem.AddMapping(partition, FATFS);
                FATFileList = FATFS.GetRoot();

            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Erreur dans la partition "+partition+" .");
                Console.ForegroundColor = ConsoleColor.White;
            }

        }
        public void ListFiles()
        {
            /*
             * List files.
             */
            for (int i = 0; i < FATFileList.Count; i++)
            {
                var xItem = FATFileList[i];
                if (xItem is Sys.Filesystem.Listing.Directory)
                {
                    Console.WriteLine("<" + FATFileList[i].Name + ">");
                }
                else if (xItem is Sys.Filesystem.Listing.File)
                {
                    Console.WriteLine();
                    Console.WriteLine(FATFileList[i].Name);
                    Console.WriteLine(FATFileList[i].Size);
                }
            }
        }
        public string getdisksize()
        {
            return (xATA.BlockCount * xATA.BlockSize / 1024 / 1024).ToString();
        }
        public string GetFile(int index)
        {
            try
            {
                /*
                 * Find the file.
                 */
                FAT.Listing.FatFile TestFile = (FAT.Listing.FatFile)FATFileList[index];
                var xStream = new Sys.Filesystem.FAT.FatStream(TestFile);
                var xData = new byte[TestFile.Size];
                xStream.Read(xData, 0, (int)TestFile.Size);
                var xText = Encoding.ASCII.GetString(xData);
                return xText;
            }
            catch
            {
                return "Error";
            }
        }
        public string ReturnFileFromName(string name)
        {
            for (int i = 0; i < FATFileList.Count; i++)
            {
                var xItem = FATFileList[i];
                if (xItem.Name == name)
                {
                    FAT.Listing.FatFile TestFile = (FAT.Listing.FatFile)xItem;
                    var xStream = new Sys.Filesystem.FAT.FatStream(TestFile);
                    var xData = new byte[TestFile.Size];
                    xStream.Read(xData, 0, (int)TestFile.Size);
                    var xText = Encoding.ASCII.GetString(xData);
                    return xText;
                }
                else
                {
                    return "Le fchier n'existe pas.";
                }
            }
            return "Erreur";

        }
    }
}
